﻿/**
 * @author zlan network
 *
 */

var ZLAN_FileControl = {
    getFileTable: function (tid) {
        //获取文件列表table元素
        return $('#' + tid, navTab.getCurrentPanel());
    },
    collect: function (param, tid) {
        var files = new Array();
        ZLAN_FileControl.getFileTable(tid).find("tbody>tr[rel]", navTab.getCurrentPanel()).each(function () {
            var file = new Object();
            file.FileId = $(this).attr('rel');
            file.FileName = $(this).find("td>input[name='filename']").attr("value");
            file.FilePath = $(this).find("td>input[name='filepath']").attr("value");
            file.FileExt = $(this).find("td>input[name='fileext']").attr("value");

            files[files.length] = file;
        });
        $("input[id=" + param + "]", navTab.getCurrentPanel()).attr("value", $.toJSON(files));
        return true;
    },
    appendExist: function (ctlid, tid, ext, max) {
        dialogReturn = new Array();
        $.pdialog.open("/SystemManage/Control/FileControl/FileList.aspx?ext=" + ext, "fileappendexist", "现有文件", { mask: true, width: 600, height: 500,
            close: function (param) {
                $.each(dialogReturn, function (i, fileObj) {
                    if (ZLAN_FileControl.checkFileMax(max, tid)) {
                        alertMsg.warn("最多只能保存" + max + "项记录。");
                        return false;
                    }
                    ZLAN_FileControl.appendRow(ctlid, fileObj, tid);
                });
                ZLAN_FileControl.getFileTable(tid).find('tbody>tr').removeClass();
                $("table.table", navTab.getCurrentPanel()).jTable();
                $("table.list", navTab.getCurrentPanel()).cssTable();
                $(".prettyPhoto").prettyPhoto();
                ZLAN_FileControl.collect(ctlid, tid);
                dialogReturn = '';
                return true;
            }
        });
    },
    appendNew: function (ctlid, tid, ext, max, cfgObj) {//增加新
        dialogReturn = new Array();
        $.pdialog.open("/SystemManage/Control/FileControl/Upload/Upload.aspx?limit=10&ext=" + ext + "&cfgjson=" + $.toJSON(cfgObj), "fileappendnew", "新增文件", { mask: true,
            close: function () {
                $.each(dialogReturn, function (i, fileObj) {
                    if (ZLAN_FileControl.checkFileMax(max, tid)) {
                        alertMsg.warn("最多只能保存" + max + "项记录。");
                        return false;
                    }
                    ZLAN_FileControl.appendRow(ctlid, fileObj, tid);
                });
                ZLAN_FileControl.getFileTable(tid).find("tbody>tr").removeClass();
                $("table.table", navTab.getCurrentPanel()).jTable();
                $("table.list", navTab.getCurrentPanel()).cssTable();
                $(".prettyPhoto").prettyPhoto();
                ZLAN_FileControl.collect(ctlid, tid);
                dialogReturn = '';
                return true;
            }
        });
    },
    appenUrl: function (ctlid, tid, ext, max, cfgObj) {
        dialogReturn = new Array();
        $.pdialog.open("/SystemManage/Control/FileControl/Upload/BindUrl.aspx", "fileappendurl", "新增路径", { mask: true,
            close: function (param) {
                $.each(dialogReturn, function (i, fileObj) {
                    if (ZLAN_FileControl.checkFileMax(max, tid)) {
                        alertMsg.warn("最多只能保存" + max + "项记录。");
                        return false;
                    }
                    ZLAN_FileControl.appendRow(ctlid, fileObj, tid);
                });
                ZLAN_FileControl.getFileTable(tid).find("tbody>tr").removeClass();
                $("table.table", navTab.getCurrentPanel()).jTable();
                $("table.list", navTab.getCurrentPanel()).cssTable();
                $(".prettyPhoto").prettyPhoto();
                ZLAN_FileControl.collect(ctlid, tid);
                dialogReturn = '';
                return true;
            }
        });
    },
    getFileSize: function (fileSize) {
        var str = "";
        if (fileSize >= 1024 * 1024 * 1024) //文件大小大于或等于1024MB   
        {
            var num = fileSize / (1024 * 1024 * 1024);
            str = num.toFixed(2).toString() + " GB";
        }
        else if (fileSize >= 1024 * 1024) //文件大小大于或等于1024KB   
        {
            var num = fileSize / (1024 * 1024);
            str = num.toFixed(2).toString() + " MB";
        }
        else if (fileSize >= 1024) //文件大小大于等于1024bytes   
        {
            var num = fileSize / (1024);
            str = num.toFixed(2).toString() + " KB";
        }
        else {
            var num = fileSize;
            str = num.toFixed(2).toString() + " bytes";
        }
        return str;
    },
    appendRow: function (ctlid, fileObj, tid) {
        //判断id是否存在
        if (ZLAN_FileControl.getFileTable(tid).find("tbody>tr[rel=" + fileObj.Id + "]").length > 0) {
            return;
        }

        var row = "<tr target='uid' rel='" + fileObj.Id + "'>";
        row += "<td height='25px'><input name='ids' value='" + fileObj.Id + "' type='checkbox'/></td>";
        row += "<td style='display: none'>";
        row += "<input name='filename' value='" + fileObj.FileName + "' type='text'/>";
        row += "<input name='filepath' value='" + fileObj.FilePath + "' type='text'/></td>";

        var i = fileObj.FileName.lastIndexOf(".");
        var ext = "";
        var imgs = "jpg,jpeg,gif,png";
        if (i >= 0) {
            ext = fileObj.FileName.substring(i + 1);
        }
        row += "<td><span class=ext_" + ext + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + fileObj.FileName + "</span> </td>";

        row += "<td>";

        if (i > 0 && imgs.indexOf(ext) >= 0) {
            row += "<a href='" + fileObj.FilePath + "' class='prettyPhoto' title='" + fileObj.FileName + "'>预览</a>";
        }
        else {
            row += "<a style='color: #C0C0C0'>预览</a>";
        }
        row += "&nbsp;";

        row += "  <a href='/SystemManage/Basics/File/Download.aspx?uid=" + fileObj.Id + "'>下载</a>&nbsp;"
        row += " <a href=\"javascript:void(0)\" onclick=\"ZLAN_FileControl.removeRow(" + ctlid + "," + fileObj.Id + ", '" + tid + "');\">移除</a>"
        row += "</td>";
        row += "</tr>";
        ZLAN_FileControl.getFileTable(tid).find("tbody>tr:first").before(row);
    },
    removeRow: function (ctlid, relid, tid) {
        alertMsg.confirm("确认要删除么?", {
            okCall: function () {
                ZLAN_FileControl.getFileTable(tid).find("tbody>tr[rel='" + relid + "']").remove();
                ZLAN_FileControl.collect(ctlid, tid);
            }
        });
        return false;
    },
    removeSelectedRows: function (ctlid, tid) {
        alertMsg.confirm("确认要删除么?", {
            okCall: function () {
                ZLAN_FileControl.getFileTable(tid).find("tbody>tr>td>input:checked[name='ids']").each(function () {
                    ZLAN_FileControl.getFileTable(tid).find("tbody>tr[rel='" + $(this).attr('value') + "']").remove();
                    ZLAN_FileControl.collect(ctlid, tid);
                });
            }
        });
        return false;
    },
    getSelcetFiles: function () {
        //dialogReturn
        $(".allfilelist>tbody>tr[rel]").each(function () {
            if ($(this).find("td>input:checked[name='ids']").attr('checked')) {
                var file = new Object();
                file.Id = $(this).attr('rel');
                file.FileName = $(this).attr("fname");
                file.FileSize = parseInt($(this).attr("fsize"));
                file.FilePath = $(this).attr("fpath");
                dialogReturn[dialogReturn.length] = file;
            }

        });
        return true;
    },
    checkFileMax: function (max, tid) {
        var count = ZLAN_FileControl.getFileTable(tid).find("tbody>tr[rel]").length;
        return count >= max;
    },
    getAppendUrl: function () {
        var have = $("#appendfileurl").find("span[id=havefile]").text();
        if (have == "0") {
            alertMsg.warn('请先增加文件！');
            return false;
        }
        else {
            var file = new Object();
            file.Id = $("#appendfileurl").find("span[id=fileid]").text();
            file.FileName = $("#appendfileurl").find("span[id=filename]").text();
            file.FileSize = $("#appendfileurl").find("span[id=filesize]").text();
            file.FilePath = $("#appendfileurl").find("span[id=filepath]").text();
            dialogReturn[dialogReturn.length] = file; 
            return true;
        }
    }
};